<?php
class BaydensController extends AppController {

	var $name = 'Baydens';
	var $helpers = array('Html', 'Form');
	var $paginate = array('limit' =>10, 'page' => 1);
/*
	function index() {
		$this->Bayden->recursive = 0;
		$this->set('baydens', $this->paginate());
	}
	*/

	function index() {
		if($this->Auth->user('ROLE') == 'admin'){
			$this->Bayden->recursive = 0;
			$this->set('baydens', $this->paginate());
		}else{
			$cbid = $this->Auth->user('canbo_id');
			$cb = $this->Canbo->find('first', array('conditions'=>array('Canbo.id'=>$cbid)));
			$this->Trambvtv->recursive = 2;
			$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
			$this->Bayden->recursive = 1;
			$this->set('baydens', $this->paginate('Bayden',array('Bayden.trambvtv_id'=>$tram['Trambvtv']['id'])));
		}
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Bayden.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('bayden', $this->Bayden->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Bayden->create();
			if ($this->Bayden->save($this->data)) {
				$this->Session->setFlash(__('Thông tin bạn nhập đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Có thể tọa độ bẫy đèn bị trùng. Bạn vui lòng nhập lại.', true));
			}
		}
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$this->Trambvtv->recursive = 1;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
//		$trambvtvs = $this->Bayden->Trambvtv->find('list',array('fields'=>array('Trambvtv.id','Ten_tr')));
		$this->set(compact('tram'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Bayden', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Bayden->save($this->data)) {
				$this->Session->setFlash(__('Thông tin bạn nhập đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Có thể tọa độ bẫy đèn bị trùng. Bạn vui lòng nhập lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Bayden->read(null, $id);
		}
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$this->Trambvtv->recursive = 1;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		//$trambvtvs = $this->Bayden->Trambvtv->find('list',array('fields'=>array('Trambvtv.id','Ten_tr')));
		$this->set(compact('tram'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Bayden', true));
			$this->redirect(array('action'=>'index'));
		}
		$Bayden=$this->Bayden->PDtBd->find('first',array('conditions'=>array('PDtBd.bayden_id'=>$id)));
		if($Bayden['PDtBd']['id']!=null)   {
			$this->Session->setFlash(__('Bạn không thể xóa Bẫy đèn này vì sẽ làm sai các dữ liệu có liên quan', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Bayden->del($id)) {
			$this->Session->setFlash(__('Thao tác xóa hoàn tất', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->Bayden->recursive = 0;
		$this->set('baydens', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Bayden.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('bayden', $this->Bayden->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Bayden->create();
			if ($this->Bayden->save($this->data)) {
				$this->Session->setFlash(__('The Bayden has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Bayden could not be saved. Please, try again.', true));
			}
		}
		$trambvtvs = $this->Bayden->Trambvtv->find('list');
		$this->set(compact('trambvtvs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Bayden', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Bayden->save($this->data)) {
				$this->Session->setFlash(__('The Bayden has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Bayden could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Bayden->read(null, $id);
		}
		$trambvtvs = $this->Bayden->Trambvtv->find('list');
		$this->set(compact('trambvtvs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Bayden', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Bayden->del($id)) {
			$this->Session->setFlash(__('Bayden deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>